package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kcwl.carrier.domain.entity.CarrierCooperateInfo;
import com.kcwl.carrier.domain.repository.ICarrierCooperateInfoRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.CarrierCooperateInfoMapper;
import com.kcwl.carrier.infrastructure.repository.po.CarrierCooperateInfoPO;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class CarrierCooperateInfoRepositoryImpl extends BaseRepositoryImpl<CarrierCooperateInfoMapper, CarrierCooperateInfoPO> implements ICarrierCooperateInfoRepository {

    @Resource
    private CarrierCooperateInfoMapper carrierCooperateInfoMapper;

    /**
     * 查询默认合作企业
     */
    @Override
    public CarrierCooperateInfo queryInitByCarrierId(Long carrierId) {
        QueryWrapper<CarrierCooperateInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assistant_id", carrierId);
        queryWrapper.eq("is_init", YesNoEnum.YEA.getValue());
        return KcBeanConverter.toBean(carrierCooperateInfoMapper.selectOne(queryWrapper), CarrierCooperateInfo.class);
    }

    @Override
    public CarrierCooperateInfo getByCarrierId(Long assistantId) {
        QueryWrapper<CarrierCooperateInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assistant_id", assistantId);
        queryWrapper.eq("deleted", 0);
        return KcBeanConverter.toBean(carrierCooperateInfoMapper.selectOne(queryWrapper), CarrierCooperateInfo.class);
    }

    /**
     * 查询运力辅助人可用合作企业列表
     */
    @Override
    public List<CarrierCooperateInfo> queryByCarrierId(Long carrierId) {
        QueryWrapper<CarrierCooperateInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assistant_id", carrierId);
        queryWrapper.eq("is_del", YesNoEnum.NAY.getValue());
        return KcBeanConverter.toList(carrierCooperateInfoMapper.selectList(queryWrapper), CarrierCooperateInfo.class);
    }

    /**
     * 查询运力辅助人所有合作企业列表
     */
    @Override
    public List<CarrierCooperateInfo> queryListByCarrierId(Long carrierId) {
        QueryWrapper<CarrierCooperateInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assistant_id", carrierId);
        return KcBeanConverter.toList(carrierCooperateInfoMapper.selectList(queryWrapper), CarrierCooperateInfo.class);
    }

    /**
     * 保存默认合作企业
     */
    @Override
    public int saveInit(CarrierCooperateInfo carrierCooperateInfo) {
        carrierCooperateInfo.setIsInit(YesNoEnum.YEA.getValue());
        return add(carrierCooperateInfo);
    }

    /**
     * 添加合作企业
     */
    @Override
    public int add(CarrierCooperateInfo carrierCooperateInfo) {
        carrierCooperateInfo.setCreateTime(new Date());
        carrierCooperateInfo.setDeleted(YesNoEnum.NAY.getValue());
        carrierCooperateInfo.setEnterFlag(YesNoEnum.NAY.getValue());
        return carrierCooperateInfoMapper.insert(KcBeanConverter.toBean(carrierCooperateInfo, CarrierCooperateInfoPO.class));
    }

    @Override
    public int updateByCarrierId(CarrierCooperateInfo carrierCooperateInfo) {
        UpdateWrapper<CarrierCooperateInfoPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("assistant_id", carrierCooperateInfo.getCarrierId());
        carrierCooperateInfo.setUpdateTime(new Date());
        return carrierCooperateInfoMapper.update(KcBeanConverter.toBean(carrierCooperateInfo, CarrierCooperateInfoPO.class), updateWrapper);
    }

    @Override
    public Class<CarrierCooperateInfoPO> getPoClass() {
        return CarrierCooperateInfoPO.class;
    }
}
